<?php
/**
 * 查询控制器
 */
namespace app\manage\controller\admin;

use app\manage\controller\Base;
use app\common\cache\CacheData;

class Handle extends Base
{

	private $params;

    public function __construct(){
        parent::__construct();
        if(false==Base::checkToken()){
            $this->error('token error!');
        }
        $params = input('post.');
        unset($params['token']);
        $this->params = $params;
    }

    public function add_admin(){
        $params = $this->params;

        $result = $this->validate($params, 'Admin.manage_add');
        if(true!==$result){
            $this->error($result);
        }

        $username = $params['username'];
        $data = $params;
        unset($data['repassword']);
        $data['password'] = hashPwd($data['password']);

        $model = new \app\common\model\Admin;
        if($model->where(['username'=>$username])->find()){
            $this->error('添加失败，管理员用户名已经存在');
        }else{
            $model->allowField(true)->save($data);
            $this->success('添加成功！');
        }
    }

    public function edit_admin(){
        $params = $this->params;

        $result = $this->validate($params, 'Admin.manage_edit');
        if(true!==$result){
            $this->error($result);
        }

        $id = $params['id'];
        
        unset($params['username']);
        unset($params['id']);
        $data = $params;
        if(isset($data['password'])){
            if(empty($data['password'])):
                unset($data['password']);
            else:
                if($params['repassword']!=$params['password']){
                    $this->error('两次密码输入不一致');
                }
                unset($data['repassword']);
                $data['password'] = hashPwd($data['password']);
            endif;
        }

        $model = new \app\common\model\Admin;
        $model->allowField(true)->save($data,['id'=>$id]);
        $this->success('修改成功！');
    }

    /**
     * 添加权限
     */
    public function add_auth(){
    	$params = $this->params;

    	$auth_name = isset($params['auth_name'])?$params['auth_name']:'';
    	$auth_path = isset($params['auth_path'])?strtolower($params['auth_path']):'';
    	if(!$auth_name||!$auth_path){
    		$this->error('提交数据错误');
    	}
    	
    	$model = new \app\common\model\AdminAuth;
    	if($model->where(['auth_path'=>$auth_path])->find()){
    		$this->error('该权限规则已经添加过');
    	}else{
    		$arr = explode('.', $auth_path);
    		$type = end($arr);
    		$model->save(['auth_name'=>$auth_name,'auth_path'=>$auth_path,'type'=>$type]);
    		$authId = $model->getLastInsId();

    		// 给超级管理员加上该规则
    		$adminGroupModel = new \app\common\model\AdminGroup;
    		$group_auth = $adminGroupModel->where(['id'=>1])->value('group_auth');
    		if($group_auth){
    			$group_auth = unserialize($group_auth);
    			$group_auth[] = $authId;
    		}else{
    			$group_auth = [$authId];
    		}
    		$group_auth = serialize($group_auth);
    		$adminGroupModel -> where(['id'=>1]) -> update(['group_auth'=>$group_auth]);

    		$CACHE = CacheData::getInstance('AdminAuth');
    		$CACHE -> updateCache();
    		$CACHE = CacheData::getInstance('AdminGroup');
    		$CACHE -> updateCache();
    		$this->success('添加成功！');
    	}

    }
    /**
     * 编辑权限
     * @return [type] [description]
     */
    public function edit_auth(){
        $params = $this->params;
        $id = isset($params['id'])?$params['id']:0;
        $auth_name = isset($params['auth_name'])?$params['auth_name']:'';
        $auth_path = isset($params['auth_path'])?$params['auth_path']:'';
        $type = isset($params['type'])?$params['type']:'';

        if(!$id||!$auth_name||!$auth_path){
            $this->error('参数错误');
        }

        $model = new \app\common\model\AdminAuth;
        if($model->where(['auth_path'=>$auth_path,'id'=>['<>',$id]])->find()){
            $this->error('该权限规则已经存在');
        }else{
            $model->where(['id'=>$id])->update([
                    'auth_name'=>$auth_name,
                    'auth_path'=>$auth_path,
                    'type'=>$type,
                ]);
            $CACHE = CacheData::getInstance('AdminAuth');
            $CACHE -> updateCache();
            $this->success('修改完成');
        }

    }

    public function del_auth(){
        $params = $this->params;
        $id = isset($params['id'])?$params['id']:0;
        
        $model = new \app\common\model\AdminAuth;
        if($model->where(['id'=>$id])->delete()){
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }

    public function add_group(){
    	$params = $this->params;
    	$group_name = isset($params['group_name'])?$params['group_name']:'';
    	$group_auth = isset($params['group_auth'])?$params['group_auth']:[];
    	if(!$group_name||!$group_auth){
    		$this->error('提交参数错误');
    	}

    	$model = new \app\common\model\AdminGroup;
    	if($model->where(['group_name'=>$group_name])->find()){
    		$this->error('用户组名称已经存在');
    	}else{
    		$model->save(['group_name'=>$group_name,'group_auth'=>serialize($group_auth)]);
    		
    		$CACHE = CacheData::getInstance('AdminGroup');
    		$CACHE -> updateCache();
    		$this->success('添加成功！');
    	}

    }

    public function edit_group(){
        $params = $this->params;
        $id = isset($params['id'])?$params['id']:'';
        $group_name = isset($params['group_name'])?$params['group_name']:'';
        $group_auth = isset($params['group_auth'])?$params['group_auth']:[];

        if(!$group_name||!$group_auth||!$id){
            $this->error('提交参数错误');
        }

        $model = new \app\common\model\AdminGroup;
        if($model->where(['group_name'=>$group_name,'id'=>['<>',$id]])->find()){
            $this->error('用户组名称已经存在');
        }else{
            $model->where(['id'=>$id])->update(['group_name'=>$group_name,'group_auth'=>serialize($group_auth)]);
            
            $CACHE = CacheData::getInstance('AdminGroup');
            $CACHE -> updateCache();
            $this->success('修改成功！');
        }

    }

    public function del_group(){
        $params = $this->params;
        $id = isset($params['id'])?$params['id']:'';
        if(!$id)
            $this->error('提交参数错误');
        if($id==1){
            $this->error('超级管理员用户组无法删除');
        }
        $model = new \app\common\model\AdminGroup;
        $group = $model->find($id);
        if($group['total']>0){
            $this->error('该用户组下存在管理用户，无法删除该组');
        }
        if($model->where(['id'=>$id])->delete()){
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }

}